import qiniu
from flask import current_app

access_key = 'W0oGRaBkAhrcppAbz6Nc8-q5EcXfL5vLRashY4SI'
secret_key = 'tsYCBckepW4CqW0uHb9RdfDMXRDOTEpYecJAMItL'
bucket_name = "python-ihome"


def pic_storage(data):
    q = qiniu.Auth(access_key,secret_key)
    token = q.upload_token(bucket_name)

    try:
        ret,info = qiniu.put_data(token,None,data)
    except Exception as e:
        current_app.logger.error(e)
        raise e

    if info.status_code != 200:
        raise Exception("上传图片到第三方平台异常")

    return ret["key"]

if __name__ == '__main__':
    file = input("请输入图片地址")
    with open(file,"rb") as f:
        pic_storage(f.read())
